package com.herui.ota.pojo.dto;

import com.alibaba.fastjson.JSON;
import com.herui.ota.entity.BaseTree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class TreeManagerWrap{

    private BaseTree BaseTree;

    public TreeManagerWrap(BaseTree BaseTree) {
        this.BaseTree = BaseTree;
    }

    public BaseTree getDeviceOtaTree() {
        return BaseTree;
    }

    // 函数用于获取指定层级的数据
    public List<BaseTree> getNodesAtLevel(int targetLevel) {
        List<BaseTree> nodesAtLevel = new LinkedList<>();
        getNodesAtLevelHelper(BaseTree, targetLevel, 1, nodesAtLevel);
        return nodesAtLevel;
    }

    // 辅助方法，递归获取指定层级的节点
    private void getNodesAtLevelHelper(BaseTree node, int targetLevel, int currentLevel, List<BaseTree> result) {
        if (node == null) {
            return;
        }
        if (currentLevel == targetLevel) {
            result.add(node);
            return;
        }
        for (BaseTree child : node.getBaseTrees()) {
            getNodesAtLevelHelper(child, targetLevel, currentLevel + 1, result);
        }
    }

    /**
     * 插入新的节点
     * @param newNode
     */
    public void insertNode(BaseTree newNode) {
        BaseTree parent = getParent(BaseTree, newNode);
        parent.getBaseTrees().add(newNode);
    }

    private BaseTree getParent(BaseTree currentNode, BaseTree newNode){
//        System.out.println(currentNode.getId()+"======="+newNode.getParentId());
        if (currentNode.getId().equals(newNode.getParentId())){
            return currentNode;
        }else{
            for (BaseTree node : currentNode.getBaseTrees()) {
                BaseTree parent = getParent(node, newNode);
                if (parent != null){
                    return parent;
                }
            }
            return null;
        }
    }


    // 构建树
    public static BaseTree buildTree(List<BaseTree> nodeList) {
        BaseTree root = null;
        for (BaseTree node : nodeList) {
            if (node.getParentId() == -1) {
                root = node;
                break;
            }
        }
        if (root == null) {
            throw new IllegalStateException("No root node found");
        }
        buildTreeHelper(root, nodeList);
        return root;
    }

    // 辅助方法，递归构建树
    private static void buildTreeHelper(BaseTree parentNode, List<BaseTree> nodeList) {
        for (BaseTree node : nodeList) {
            if (node.getParentId().equals(parentNode.getId())) {
                parentNode.getBaseTrees().add(node);
                buildTreeHelper(node, nodeList);
            }
        }
    }

    /**
     * 返回那一层不匹配
     * @param userInput  品牌1-市场信息1-系列序列号版本1
     * @return
     */
    public int[] findMissingLevel(String userInput) {
        String[] inputParts = userInput.split("-");
        BaseTree currentLevel = BaseTree;

        for (int i = 0; i < inputParts.length; i++) {
            String part = inputParts[i];
            boolean found = false;
            for (BaseTree node : currentLevel.getBaseTrees()) {
                if (part.equals(node.getNodeData())) {
                    currentLevel = node;
                    found = true;
                    break;
                }
            }
            if (!found) {
                return new int[]{i, currentLevel.getId()};
            }
        }
        return new int[]{-1, -1};
    }

    public static List<BaseTree> createMockData() {
        List<BaseTree> mockData = new ArrayList<>();

        mockData.add(new BaseTree(0, -1, "root", 0));

        // 第一组模拟数据
        mockData.add(new BaseTree(1, 0, "品牌1", 1));
        mockData.add(new BaseTree(2, 1, "市场信息1", 2));
        mockData.add(new BaseTree(3, 2, "系列序列号版本1", 3));
        mockData.add(new BaseTree(4, 3, "设备类型1", 4));
        mockData.add(new BaseTree(5, 4, "渠道1", 5));

        // 第二组模拟数据
        mockData.add(new BaseTree(6, 0, "品牌2", 1));
        mockData.add(new BaseTree(7, 6, "市场信息2", 2));
        mockData.add(new BaseTree(8, 7, "系列序列号版本2", 3));
        mockData.add(new BaseTree(9, 8, "设备类型2", 4));
        mockData.add(new BaseTree(10, 9, "渠道2", 5));

        // 第三组模拟数据
        mockData.add(new BaseTree(11, 0, "品牌3", 1));
        mockData.add(new BaseTree(12, 11, "市场信息3", 2));
        mockData.add(new BaseTree(13, 12, "系列序列号版本3", 3));
        mockData.add(new BaseTree(14, 13, "设备类型3", 4));
        mockData.add(new BaseTree(15, 14, "渠道3", 5));
        return mockData;
    }

    /**
     * 打印整棵树
     * @param root
     */
    public static void printTree(BaseTree root) {
        printTreeNode(root, 0);
    }

    private static void printTreeNode(BaseTree node, int level) {
        if (node == null) {
            return;
        }

        // 打印当前节点
        for (int i = 0; i < level; i++) {
            System.out.print("  "); // 缩进，用两个空格表示一层级
        }
//		System.out.println("Node ID: " + node.getId() + ", Parent ID: " + node.getParentId() + ", Node Data: " + node.getNodeData() + ", Level: " + node.getLevel());
        System.out.println(node.getNodeData());
        // 打印当前节点的子节点
        for (BaseTree child : node.getBaseTrees()) {
            printTreeNode(child, level + 1); // 递归打印子节点
        }
    }

    public static void main(String[] args) {
        List<BaseTree> mockData = TreeManagerWrap.createMockData();
        BaseTree BaseTree = TreeManagerWrap.buildTree(mockData);
        TreeManagerWrap.printTree(BaseTree);

        TreeManagerWrap treeManagerWrap = new TreeManagerWrap(BaseTree);

        List<BaseTree> nodesAtLevel = treeManagerWrap.getNodesAtLevel(1);
        for (BaseTree node: nodesAtLevel){
            System.out.println("Node ID: " + node.getId() + ", Parent ID: " + node.getParentId() + ", Node Data: " + node.getNodeData() + ", Level: " + node.getLevel());
        }

        int[] missingLevel = treeManagerWrap.findMissingLevel("品牌4-市场信息1-系列序列号版本1");
        System.out.println(JSON.toJSON(missingLevel));

//        treeManagerWrap.insertNode(new BaseTree(33, 11, "市场信息33", 2));
    }
}
